package cn.tedu._03vehicle.base.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

import java.time.LocalDateTime;

/**
 * 限定访问时间的拦截器:所有请求只能在06:00-23:00之间访问
 */
public class TimeAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        int hour = now.getHour();
        if (hour < 6 || hour > 22) {
            //拦截后重定向到error.html页面
           /* response.sendRedirect("/error.html");
            return false;*/
            //拦截后抛出异常使用全局异常处理器处理
            throw new RuntimeException("请在06:00-23:00之间进行访问");
        }
        return true;
    }
}
